package com.jiayou.activity.adapter

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.RecyclerView
import cn.hutool.http.HttpRequest
import com.jiayou.R
import com.jiayou.activity.ChatActivity
import com.jiayou.activity.MainActivity
import com.jiayou.message.ChannelMessage
import com.jiayou.message.TextMessage
import com.jiayou.message.type.DataType
import com.jiayou.tool.SerializableTool
import de.hdodenhof.circleimageview.CircleImageView
import kotlinx.android.synthetic.main.menu_item.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.time.LocalDateTime
import kotlin.concurrent.thread

class UserAdapter(var context: Context, var list: ArrayList<User>) :
    RecyclerView.Adapter<UserAdapter.UserViewHolder>() {


    class UserViewHolder(
        view: View
    ) : RecyclerView.ViewHolder(view) {
        var lin: LinearLayout? = null
        var head: CircleImageView? = null
        var nicheng: TextView? = null
        var qianming: TextView? = null
        var id: String? = ""

        init {
            lin = view as LinearLayout
            head = view.findViewById(R.id.head)
            nicheng = view.findViewById(R.id.nicheng)
            qianming = view.findViewById(R.id.qianming)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        return UserViewHolder(
            LayoutInflater.from(context).inflate(
                R.layout.user_item,
                parent,
                false
            )
        )
    }


    override fun getItemCount() = list.size

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        holder.id = list[position].id
        holder.lin?.setOnClickListener {
            val intent = Intent(context, ChatActivity::class.java)
            intent.putExtra("id", list[position].id)
            intent.putExtra("name", list[position].name)
            context.startActivity(intent)
        }
        val user = list[position]
        holder.nicheng?.text = user.name
        holder.qianming?.text = user.description
        thread {
            val bodyStream =
                HttpRequest.get("http://${(context as MainActivity).uri}/im/userinfo/getImgByName").form(
                    "img",
                    user.img
                )
                    .header("auth", "73913da7-508b-4646-b8ae-ef6458f41153")
                    .execute()
                    .bodyStream()

            runBlocking {
                withContext(Dispatchers.Main) {
                    holder.head?.setImageBitmap(BitmapFactory.decodeStream(bodyStream))
                }
            }
        }
    }
}